import Vue from 'vue'
import axios from "axios";
import {
    Loading,
    Notification,
    Message
} from 'element-ui';

Vue.prototype.$notify = Notification;

Vue.prototype.$message = Message;
var loading;
var time;
var ss = 0;

function startLoading() { //使用Element loading-start 方法
    loading = Loading.service({
        lock: true,
        text: '嗷里个嗷，拼命加载中...',
        spinner: 'el-icon-loading',
        background: 'rgba(0, 0, 0, 0.7)'
    });
    time = setInterval(() => {
        ss++
        // console.log(ss);
        if (ss >= 6) {
            // var vm = new Vue()
            //     // console.log(vm);
            // vm.$notify.error({
            //     title: '错误',
            //     message: '请求超时，请检查网络是否连接！',
            // });
            // ss = 0
            endLoading();
        }
    }, 1000)
}

function endLoading() { //使用Element loading-close 方法
    clearInterval(time)
    loading.close()


}
axios.interceptors.request.use(config => {
    startLoading();
    return config
})

axios.interceptors.response.use(config => {
    endLoading();
    return config
})

let Request = (params) => {
    return new Promise((resolve, reject) => {
        axios({
                ...params
            })
            .then((res) => {
                resolve(res)
            })
            .catch((error) => {
                reject(error)
            })
    })
}

export {
    Request
}